const app = getApp()
import { getUserInfo, getMaterialRecommendList, getCaseRecommendList, getMaterialList, getCaseList, shareQrCode, getPictureList } from '../../api/index'
// import { getMaterialRecommendList, getCaseRecommendList } from '../../api/index'

Page({
	data: {
    imgSrc: 'https://ja-material-formal.oss-cn-shenzhen.aliyuncs.com/Material/2022-03-04/25f4e17a14a248fdb133bc0fd7040dfd.jpg',
    activeTab: 'index',
    code: '',
    mobilecode: '',
    reachBottomTime: 0,
    shareModal: false,
    showPoster: false,
    showBack: false,
    topHeight: 0,
    showCollect: false,
    showAuthor: false,
    onshowTime: 0,
    pageSize: 15,
    recommend: {
      videoBanners: [],
      materialBanners: [],
      caseBanners: [],
      caseList: [],
      materialList: []
    },
    material: {
      currentPage: 1,
      isEmpty: false,
      list: []
    },
    case: {
      currentPage: 1,
      isEmpty: false,
      list: []
    },
    isFirst: {
      index: false,
      material: false,
      case: false,
      attention: false
    },
    userInfo: {}
  },
  changeFirstStatus ({ detail }) {
    this.setData({ ['isFirst.' + detail]: true })
  },
  changeTab ({ target: { dataset: { key } } }) {
    this.setData({
      reachBottomTime: 0,
      activeTab: key
    })
    this.getRequest()
  },
  onPullDownRefresh () {
    if (this.data.activeTab === 'index') {
      this.setData({ 'isFirst.index': false })
    } else if (this.data.activeTab === 'material') {
      this.setData({ 'isFirst.material': false })
    } else if (this.data.activeTab === 'case') {
      this.setData({ 'isFirst.case': false })
    } else {
      this.setData({
        onshowTime: new Date().getTime()
      })
    }
    this.getRequest()
  },
  onReachBottom () {
    this.setData({
      reachBottomTime: new Date().getTime()
    })
  },
  onPageScroll ({ scrollTop }) {
    const { screenHeight } = app.globalData.systemInfo
    if (scrollTop >= screenHeight) {
      this.setData({ showBack: true })
    } else {
      this.setData({ showBack: false })
    }
  },
  async onShareAppMessage () {
    try {
      wx.showLoading({ title: '正在生成分享...', mask: true })
      const { ResultSet } = await shareQrCode({ scene: '1' })
      wx.hideLoading()
      this.setData({ shareModal: false })
      return {
        title: `${ResultSet.userData.title}`,
        path: `/pages/index/index`,
        imageUrl: ResultSet.userData.img
      }
    } catch (err) {
      wx.hideLoading()
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  goTop () {
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showToast({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新版本后重试！'
      })
    }
  },
  goSearch () {
    wx.$nav.navigateTo('/pages/search/index')
  },
  sharePoster () {
    this.setData({
      shareModal: true
    })
  },
  closeShareModal () {
    this.setData({ shareModal: false })
  },
  showPosterModal () {
    this.setData({ shareModal: false, showPoster: true })
  },
  closePosterModal () {
    this.setData({ showPoster: false })
  },
  async closeCollect () {
    await wx.$pro.showTabBar()
    await wx.$pro.setStorageSync('FIRSTLOGIN', false)
    this.setData({ showCollect: false })
  },
  async getUserInfo () {
    try {
      const { ResultSet } = await getUserInfo({})
      app.globalData.userInfo = ResultSet.userData.userInfo ? ResultSet.userData.userInfo : {}
      this.setData({ userInfo: ResultSet.userData.userInfo })
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors })
    }
  },
  async getMaterialRecommendList () {
    try {
      const { ResultSet } = await getMaterialRecommendList()
      this.setData({ 'recommend.materialList': ResultSet.userData.data, 'isFirst.index': true })
      wx.stopPullDownRefresh()
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  async getCaseRecommendList () {
    try {
      wx.showLoading({ title: '加载中...', mask: true })
      const { ResultSet } = await getCaseRecommendList()
      this.setData({ 'recommend.caseList': ResultSet.userData.data })
      await this.getMaterialRecommendList()
      wx.hideLoading()
    } catch (err) {
      wx.hideLoading()
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  async getVideoCaseList () {
    try {
      const { ResultSet } = await getCaseRecommendList({ type: 1 })
      this.setData({ 'recommend.videoList': ResultSet.userData.data })
      // this.getMaterialList()
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  async getMaterialList () {
    try {
      wx.showLoading({ title: '加载中...', icon: 'none' })
      const { ResultSet } = await getMaterialList({ currentPage: 1, pageSize: this.data.pageSize })
      this.data.material.isEmpty = !ResultSet.userData.data.length
      if (ResultSet.userData.data.length) {
        this.data.material.currentPage = 2
      }
      this.data.material.list = ResultSet.userData.data
      wx.stopPullDownRefresh()
      wx.hideLoading()
      this.setData({ material: this.data.material, 'isFirst.material': true })
    } catch (err) {
      wx.hideLoading()
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  changeMaterial ({ detail }) {
    this.setData({ material: detail })
  },
  async getCaseList () {
    try {
      wx.showLoading({ title: '加载中...', mask: true })
      const { ResultSet } = await getCaseList({ currentPage: 1, pageSize: this.data.pageSize })
      this.data.case.isEmpty = !ResultSet.userData.data.length
      if (ResultSet.userData.data.length) {
        this.data.case.currentPage = 2
      }
      this.data.case.list = ResultSet.userData.data
      wx.stopPullDownRefresh()
      wx.hideLoading()
      this.setData({ case: this.data.case, 'isFirst.case': true })
    } catch (err) {
      wx.hideLoading()
      this.setData({ 'case.list': [], 'case.isEmpty': true })
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  changeCase ({ detail }) {
    this.setData({ case: detail })
  },
  getRequest () {
    if (this.data.activeTab === 'index' && !this.data.isFirst.index) {
      this.getCaseRecommendList()
      this.getVideoCaseList()
    } else if (this.data.activeTab === 'material' && !this.data.isFirst.material) {
      this.getMaterialList()
    } else if (this.data.activeTab === 'case' && !this.data.isFirst.case) {
      this.getCaseList()
    }
  },
  async getBannerList () {
    try {
      const { ResultSet } = await getPictureList()
      this.setData({
        'recommend.videoBanners': ResultSet.userData.videoCases,
        'recommend.materialBanners': ResultSet.userData.hostMaterials,
        'recommend.caseBanners': ResultSet.userData.hostCases
      })
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  async onLoad ({ activeTab }) {
    try {
      if (activeTab) this.setData({ activeTab })
      let query = this.createSelectorQuery()
      query.select('.index-top').boundingClientRect(rect=>{
        let height = rect.height
        this.setData({ topHeight: height })
      }).exec()
      this.getBannerList()
      this.getRequest()
    } catch (err) {
      console.log(err)
    }
  },
  async onShow () {
    const firstLogin = await wx.$pro.getStorageSync('FIRSTLOGIN')
    if (!!firstLogin) wx.hideTabBar()
    this.setData({ showCollect: !!firstLogin })
    this.getUserInfo()
  }
})
